package keni.html.android.framework.connectionChannel.http;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import keni.html.android.framework.connectionChannel.Header;
import keni.html.android.framework.connectionChannel.IChannelSink;
import keni.html.android.framework.connectionChannel.RequestMessage;
import keni.html.android.framework.connectionChannel.ResponseMessage;
import keni.html.android.framework.connectionChannel.SinkBase;
import keni.html.android.framework.util.Constants;

public class HttpTransportSink  extends SinkBase{


	@Override
	public void   doProcessRequest(RequestMessage message, Header header,ResponseMessage outResponseMsg, Header outResponseHeaders){
		HttpResponse response;
		try {
			response = SetupWebRequest(message, header);
			for (org.apache.http.Header httpHeader : response.getAllHeaders()) {
				
				outResponseHeaders.add(httpHeader.getName(), httpHeader.getValue());
				
			} 
			outResponseHeaders.setContentType(response.getEntity().getContentType().getValue());
			outResponseMsg.setResponseStream(response.getEntity().getContent());
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	
private HttpResponse SetupWebRequest(RequestMessage message,Header header) throws ClientProtocolException, IOException {
		
		String method=header.get(Constants.Http.METHOD);
		HttpClient httpClient=new DefaultHttpClient();
		HttpResponse response=null;
		HttpRequestBase request=null;
		if(method.equalsIgnoreCase(Constants.Http.GET))
		{
			request=new HttpGet(header.getRequestUri());
		}
		else {
			HttpPost httpPost=new HttpPost(header.getRequestUri());
			MultipartEntity multipartEntity=new MultipartEntity();
			
			
			for (Map.Entry<String, String> property : message.getProperties().entrySet()) {
				StringBody stringBody=new StringBody(property.getValue());
				multipartEntity.addPart(property.getKey(), stringBody);
			}
			
			for (Map.Entry<String, InputStream> file : message.GetFileStreams().entrySet()) {
				InputStreamBody inputStreamBody=new InputStreamBody(file.getValue(), file.getKey());
				multipartEntity.addPart(file.getKey(), inputStreamBody);
				
			}
			httpPost.setEntity(multipartEntity);
			request=httpPost;
			
		}
		for (Map.Entry<String, String> item : header.getHeaderItems().entrySet()) {
			request.addHeader(item.getKey(), item.getValue());
		}
		
		response=httpClient.execute(request);
		
		
		return response;
		
	}



public void processResponse(RequestMessage requestMsg, Header requestHeaders,
		ResponseMessage responseMsg, Header responseHeaders) {
	
	IChannelSink preChannelSink=getPreviousChannelSink();
	
	if(preChannelSink!=null)
	{
		preChannelSink.processResponse(requestMsg, requestHeaders, responseMsg, responseHeaders);
	}
	
	
}

public Map<String, String> getProperties() {
	// TODO Auto-generated method stub
	return null;
}

}
